var config = require('./config/service');
var baseWebpackConfig = require('./bin/webpack.base.conf');
/*
 ========================================
 =            Requiring stuffs            =
 ========================================*/
var gulp = require('gulp');
var rimraf = require('gulp-rimraf');
var zip = require('gulp-zip');
var fs = require('fs');
var request = require('request');
var inquirer = require('inquirer');
var JSESSIONID = '';
var data = {};
var upload_modules = [];
var defaultModuleNamesFlag = true;
/*
 ================================================
 =            Report Errors to Console            =
 ================================================*/
gulp.on('error', function(e) {
  throw (
    e);
});
/*
 ====================================
 =            clean dist Task            =
 ====================================*/
gulp.task('clean_dist', function() {
  return gulp.src(
    ['./zip'], {read: false}
  ).pipe(rimraf());
});
// 是否需要全选
gulp.task('selectAll', function(done) {
  inquirer.prompt(
    [{
      type: 'confirm',
      name: 'defaultModuleNames',
      message: '是否全选  ()',
      default: true
    }]
  ).then(function(answers) {
    defaultModuleNamesFlag = answers.defaultModuleNames;
    done();
  });
});
/*
 ====================================
 =             Task            =
 ====================================*/
gulp.task('select', ['selectAll'], function(done) {
  var moduleNames = [];
  var defaultModuleNames = [];
  for (var name in baseWebpackConfig.entry) {
    moduleNames.push(name);
  }
  if (defaultModuleNamesFlag) {
    defaultModuleNames = moduleNames;
  }
  inquirer.prompt([
    {
      type: 'checkbox',
      name: 'moduleNames',
      message: '选择要上传的模块:',
      choices: moduleNames,
      default: defaultModuleNames
    }, {
      type: 'confirm',
      name: 'public',
      message: '是否需要上传公共包(public)',
      default: true
    }]
  ).then(function(answers) {
    upload_modules = answers.moduleNames;
    console.log('选择的包' + upload_modules);
    if (answers.public) {
      upload_modules.push(config.publicName);
    }
    done();
  });
});
/*
 ====================================
 =            zip Task            =
 ====================================*/
gulp.task('zip', ['clean_dist'], function(done) {
  for (var key in baseWebpackConfig.entry) {
    gulp.src(['!**/*.map', './' + config.dest + '/' + key + '/**'], {base: config.dest})
    .pipe(zip(key + '.zip'))
    .pipe(gulp.dest('./zip'));
  }
  gulp.src(['!**/*.map', './' + config.dest + '/public/**'], {base: config.dest})
  .pipe(zip('public.zip'))
  .pipe(gulp.dest('./zip'));
  done();
});
/*
 ====================================
 =         login Task            =
 ====================================*/
gulp.task('login', ['select'], function(done) {
  request(
    {
      url: config.url + config.urlParam + '/login',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      rejectUnauthorized: false,
      form: {
        username: config.loginfo.username,
        password: config.loginfo.password
      }
    },
    function(err, httpResponse, body) {
      console.log(JSON.stringify(httpResponse));
      if (err) {
        return console.error('failed:', err);
      }
      JSESSIONID = httpResponse.headers['set-cookie'][0].split(';')[0].split('=')[1];
      done();
    }
  );
});
/*
 ====================================
 =         upload zip Task            =
 ====================================*/
gulp.task('upload', ['getFid'], function(done) {
  var cookie = request.cookie('JSESSIONID=' + JSESSIONID);
  var j = request.jar();
  var count = 0;
  j.setCookie(cookie, config.url + config.urlParam + '/appstore/func-version/create');
  for (var i = 0, l = upload_modules.length; i < l; i++) {
    var module = upload_modules[i];
    console.log('准备上传模块' + module);
    if (!data[module]) {
      continue;
    }
    var formData = {
      clientVersion: '',
      id: data[module],
      interfaceUrl: '',
      'function.id': data[module],
      remark: '',
      'function.unzip': 'N',
      workStatus: 'ENABLE',
      zipUrl: '',
      versionFile: {
        value: fs.createReadStream(__dirname + '/zip/' + module + '.zip'),
        options: {
          filename: module + '.zip',
          contentType: 'application/x-zip-compressed'
        }
      }
    };
    request.post({
      url: config.url + config.urlParam + '/appstore/func-version/create',
      rejectUnauthorized: false,
      formData: formData,
      jar: j
    }, function optionalCallback(err, httpResponse, body) {
      if (err) {
        return console.error('failed:', err);
      }
      console.log('successful!' + body + ' has upload');
      count += 1;
      if (count === l) {
        done();
      }
    });
  }
});
gulp.task('getFid', ['login'], function(done) {
  console.log('会话ID  JSESSIONID=' + JSESSIONID);
  var j = request.jar();
  var cookie = request.cookie('JSESSIONID=' + JSESSIONID);
  j.setCookie(cookie, config.url + config.urlParam + '/appstore/function');
  request({
    url: config.url + config.urlParam + '/appstore/function',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Referer': config.url + config.urlParam + '/appstore/function'
    },
    jar: j,
    method: 'POST',
    rejectUnauthorized: false,
    form: {
      'sEcho': '1',
      'iColumns': '7',
      'sColumns': '',
      'iDisplayStart': '0',
      'iDisplayLength': '100',
      'mDataProp_0': 'id',
      'mDataProp_1': 'icon',
      'mDataProp_2': 'appName',
      'mDataProp_3': 'appStatus',
      'mDataProp_4': 'lastUpdatedDate',
      'mDataProp_5': 'isRecommend',
      'mDataProp_6': 'id',
      'sSearch': '',
      'bRegex': 'false',
      'sSearch_0': '',
      'bRegex_0': 'false',
      'bSearchable_0': 'true',
      'sSearch_1': '',
      'bRegex_1': 'false',
      'bSearchable_1': 'true',
      'sSearch_2': '',
      'bRegex_2': 'false',
      'bSearchable_2': 'true',
      'sSearch_3': '',
      'bRegex_3': 'false',
      'bSearchable_3': 'true',
      'sSearch_4': '',
      'bRegex_4': 'false',
      'bSearchable_4': 'true',
      'sSearch_5': '',
      'bRegex_5': 'false',
      'bSearchable_5': 'true',
      'sSearch_6': '',
      'bRegex_6': 'false',
      'bSearchable_6': 'true',
      'sSearch_7': '',
      'bRegex_7': 'false',
      'bSearchable_7': 'true',
      'iSortCol_0': '1',
      'sSortDir_0': 'asc',
      'iSortingCols': '1',
      'bSortable_0': 'false',
      'bSortable_1': 'false',
      'bSortable_2': 'true',
      'bSortable_3': 'true',
      'bSortable_4': 'true',
      'bSortable_5': 'true',
      'bSortable_6': 'true',
      'bSortable_7': 'false'
    }
  }, function(err, httpResponse, body) {
    if (err) {
      return console.error('failed:', err);
    }
    if (!err && httpResponse.statusCode === 200) {
      body = JSON.parse(body);
      for (var i = 0, l = body.aaData.length; i < l; i++) {
        data[body.aaData[i].appCode.trim()] = body.aaData[i].id;
      }
      done();
    }
  });
});
